package com.zj.sa_token.util.date;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 *返回未来的某一天和今天所差的日期数
 *注意，这里要clone一个新的日期以免对原始日期类造成的修改。
 *而在daysBetween(GregorianCalendarpFormer,GregorianCalendarpLatter)就
 *直接处理而不进行clone动作，因为这里已经做了:)
 **/
public class DateFacs {
    //日志
    private  static Log logger = LogFactory.getLog(DateFacs.class);
    //比较和今天相差的天数
    public static int daysBetween(GregorianCalendar pFurtherDay){
        GregorianCalendar vToday = new GregorianCalendar();
        GregorianCalendar vFurtherDay = (GregorianCalendar) pFurtherDay.clone();
        return daysBetween(vFurtherDay,vToday);
    }
    /**上面函数的String版本　**/
    public static int daysBetween(String pFurtherDayStr){
        GregorianCalendar vFurtherDay = DateFacs.parse2Cal(pFurtherDayStr);
        GregorianCalendar vToday = new GregorianCalendar();
        return daysBetween(vFurtherDay,vToday);
    }


    /**返回较晚的时间(latter)与较早的时间(former)所差的天数**/
    public static int daysBetween(String pFormerStr,String pLatterStr){
        GregorianCalendar pFormer = DateFacs.parse2Cal(pFormerStr);
        GregorianCalendar pLatter = DateFacs.parse2Cal(pLatterStr);
        return daysBetween(pFormer,pLatter);
    }
    private static GregorianCalendar parse2Cal(String dateStr){
        String sdf="yyyy-MM-dd hh:mm:ss";
        if(dateStr.trim().length()<=10) sdf="yyyy-MM-dd";
        SimpleDateFormat timeFormat = new SimpleDateFormat(sdf);
        GregorianCalendar calendar  = new GregorianCalendar();
        try{
            Date date = timeFormat.parse(dateStr);
            date = new Date(date.getTime());
            calendar.setTime(date);
        }catch(Exception ex){
            logger.error("error in parse2Cal:" + ex.toString());
            //  System.out.println("error in DateFacs.daysBetween():"+ex.toString());
        }
        return calendar;
    }

    /**返回较晚的时间(latter)与较早的时间(former)所差的天数**/
    public static int daysBetween(GregorianCalendar pFormer,GregorianCalendar pLatter){
        GregorianCalendar vFormer = pFormer,vLatter = pLatter;
        boolean vPositive = true;
        if( pFormer.before(pLatter) ){
            vFormer = pFormer;
            vLatter = pLatter;
        }else{
            vFormer = pLatter;
            vLatter = pFormer;
            vPositive = false;
        }

        vFormer.set(Calendar.MILLISECOND,0);
        vFormer.set(Calendar.SECOND,0);
        vFormer.set(Calendar.MINUTE,0);
        vFormer.set(Calendar.HOUR_OF_DAY,0);
        vLatter.set(Calendar.MILLISECOND,0);
        vLatter.set(Calendar.SECOND,0);
        vLatter.set(Calendar.MINUTE,0);
        vLatter.set(Calendar.HOUR_OF_DAY,0);

        int vCounter = 0;

        while(vFormer.before(vLatter)){
            vFormer.add(Calendar.DATE, 1);
            vCounter++;
        }
        if( vPositive)
            return vCounter;
        else
            return -vCounter;
    }
    public static String getTime(String paTimePat){
        Calendar rightNow = Calendar.getInstance();
        SimpleDateFormat sdf=new SimpleDateFormat(paTimePat);
        String ls_Date=sdf.format(rightNow.getTime());
        return 	ls_Date;

    }
    public static String getTime(){
        Calendar rightNow = Calendar.getInstance();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ls_Date=sdf.format(rightNow.getTime());
        return 	ls_Date;
    }
    public static String getMsTime(){
        Calendar rightNow = Calendar.getInstance();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        String ls_Date=sdf.format(rightNow.getTime());
        return 	ls_Date;
    }
    //返回long型的毫秒
    public static long getMsTimeLong(){
        Calendar rightNow = Calendar.getInstance();
        return rightNow.getTimeInMillis();
    }
    //传入参数为两个字符串，分别表示日期和时间:   paDate:20100613 paTime:161202
    //返回该日期和当前系统时间相差的秒数
    //返回值：-1 表示：传入的参数异常
    public static long getSecToNow(String paDate,String paTime){
        long li_sec=0;//返回相差的秒数
        try{
            Calendar now = Calendar.getInstance();//现在的时间
            Calendar last = Calendar.getInstance();
            int year = Integer.parseInt(paDate.substring(0,4));
            int month =Integer.parseInt(paDate.substring(4,6));
            int day = Integer.parseInt(paDate.substring(6,8));
            int hour = Integer.parseInt(paTime.substring(0,2));
            int min = Integer.parseInt(paTime.substring(2,4));
            int sec = Integer.parseInt(paTime.substring(4,6));
            last.set(year, month, day, hour, min, sec);
            last.add(Calendar.MONTH,-1);//Calendar对象的中的月份是以0开始的。我们说的10月在Calendar对象中应为09月
            // System.out.println(now.getTimeInMillis());
            //System.out.println(last.getTimeInMillis());
            li_sec=(now.getTimeInMillis()-last.getTimeInMillis())/1000;
            //传入的参数异常，则返回-1;
        }catch(Exception e){
            logger.error("error  in getSecToNow :" + e.toString());
            li_sec=-1;
        }
        finally{
            return li_sec;
        }
    }
    //返回当前年份
    public static int  getYear(){
        Calendar now = Calendar.getInstance();//现在的时间
        return now.get(Calendar.YEAR);
    }
    //返回当前月份
    public static int  getMonth(){
        Calendar now = Calendar.getInstance();//现在的时间
        return now.get(Calendar.MONTH)+1;
    }
    //返回当前日
    public static int  getDay(){
        Calendar now = Calendar.getInstance();//现在的时间
        return now.get(Calendar.DATE);
    }
    //返回当前时
    public static int  getHours(){
        Calendar now = Calendar.getInstance();//现在的时间
        return now.get(Calendar.HOUR_OF_DAY);
    }
    //返回当前分
    public static int  getMinutes(){
        Calendar now = Calendar.getInstance();//现在的时间
        return now.get(Calendar.MINUTE);
    }
    //返回当前秒
    public static int  getSeconds(){
        Calendar now = Calendar.getInstance();//现在的时间
        return now.get(Calendar.SECOND);
    }

    public static void main(String[] args){
//    	 String date1="2009-03-12";
//    	 String date2="2008-10-22";
//    	 System.out.print("dif="+DateFacs.daysBetween(date1));
//    	 System.out.println(DateFacs.getMsTime());
        System.out.println(getSecToNow("20100621","105200"));
        System.out.println(Calendar.YEAR+","+getYear()+","+getMonth()+","+getDay());


    }


}
